package org.mch.comm.entity;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@JsonInclude(Include.NON_NULL)
public abstract class MBizEO {

	// JSON解析忽略该字段
	@JsonIgnore
	private Map<String, Class<?>> filedClasss = new HashMap<String, Class<?>>();

	// JSON解析忽略该字段
	@JsonIgnore
	private List<String> fieldNames = new ArrayList<String>();

	private int status;

	/**
	 * 排除的字段，不属性实体的属性；
	 */
	public static final Set<String> PcFieldSet = new HashSet<String>();
	static {
		PcFieldSet.add("fieldNames");
		PcFieldSet.add("filedClasss");
		PcFieldSet.add("filedValue");
		PcFieldSet.add("tableName");
		PcFieldSet.add("pKFieldName");
		PcFieldSet.add("class");
	}

	public MBizEO() {
		super();
		initEO();
	}

	private void initEO() {
		/*
		 * 初始化时，获得当前实体所有属性，并存储；
		 */
		PropertyDescriptor[] pros = BeanUtilsBean.getInstance().getPropertyUtils().getPropertyDescriptors(this);
		for (PropertyDescriptor prod : pros) {
			if (PcFieldSet.contains(prod.getName())) {
				continue;
			}
			fieldNames.add(prod.getName());
			filedClasss.put(prod.getName(), prod.getPropertyType());
		}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (String fieldName : fieldNames) {
			sb.append(fieldName);
			sb.append("=");
			try {
				sb.append(BeanUtils.getProperty(this, fieldName));
			} catch (Exception e) {
				e.printStackTrace();
			}
			sb.append("\n");
		}
		return sb.toString();
	}

	public String[] getFieldNames() {
		return fieldNames.toArray(new String[0]);

	}

	public Class<?> getFieldClass(String fieldName) {
		return filedClasss.get(fieldName);
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	@JsonIgnore
	public abstract String getTableName();

	@JsonIgnore
	public abstract String getPKFieldName();

	@JsonIgnore
	public String getParentPKFieldName() {
		return null;
	}

}
